/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.api.common.msg;

import java.util.Iterator;
import java.util.List;

import nanoxml.XMLElement;
import com.dg.gaming.api.common.PlayerList;
import com.dg.gaming.api.common.Table;

/**
 * Small communication object for joining an existing table.
 *
 * @author Bob Marks
 * @version Alpha 0.2.3
 */
public class MsgJoinTable extends MsgTableMessage {

    private PlayerList playerList = null;

    private XMLElement dgModelXML = null;

    /**
     * Constructor for client requesting joining an existing table.
     */
    public MsgJoinTable(String tableNum) {
        super();

        // Call set table num manually as this is called from DGClientFrame
        setTableNum(tableNum);
    }

    /**
     * Constructor from the server to the joining client.
     *
     * @param username   Name of username who has just joined.
     * @param table      Table object that the user is joining.
     */
    public MsgJoinTable(long userid, Table table) {
        super(userid);

        // Set table number manually
        setTableNum(table.getTableNum());

        // Set player list
        this.playerList = table.getPlayerList();
        if (table.getModel() != null) {
            this.dgModelXML = table.getModel().flatten();
        }
    }

    /**
     * Constructor for a server responding to other clients.
     *
     * @param username   Name of username who has just joined.
     * @param tableNum      Table number of table user is joining.
     */
    public MsgJoinTable(long userid, String tableNum) {
        super(userid);

        // Call set table number manually
        setTableNum(tableNum);
    }

    /**
     * Constructor which takes an XMLElement.
     *
     * @param message
     */
    public MsgJoinTable(XMLElement message) {
        super(message);

        List children = message.getChildren();
        for (Iterator i = children.iterator(); i.hasNext(); ) {
            XMLElement e = (XMLElement) i.next();
            String name = e.getName();
            if (name.equals(Msg.PLAYER_LIST)) {
                this.playerList = new PlayerList(e);
            } else if (name.equals(Msg.MODEL)) {
                this.dgModelXML = e;
            }
        }
    }

    /**
     * Return the player list.
     *
     * @return   Player list.
     */
    public PlayerList getPlayerList() {
        return playerList;
    }

    /**
     * Return the model state.
     *
     * @return
     */
    public XMLElement getModelState() {
        return dgModelXML;
    }

    /**
     * Return true if this message contains a model.
     *
     * @return
     */
    public boolean containsModel() {
        return (dgModelXML != null);
    }

    /**
     * Flatten the String for transmission purposes.
     *
     * @see com.dg.gaming.api.common.msg.ITransmittable#flatten()
     */
    public XMLElement flatten() {
        XMLElement message = super.flatten(Msg.JOIN_TABLE);

        if (playerList != null) {
            message.addChild(playerList.flatten());
        }
        if (dgModelXML != null) {
            message.addChild(dgModelXML);
        }

        return message;
    }
}
